//
//  UIViewExtention.m
//  ocdemo
//
//  Created by JieNing.Zhang on 2023/4/4.
//  Copyright © 2023 zjn. All rights reserved.
//

#import "UIView+Extention.h"

@implementation UIView (Extention)

- (CGSize)size;
{
    return [self frame].size;
}
 
- (void)setSize:(CGSize)size;
{
    CGPoint origin = [self frame].origin;
    [self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
}
 
-(CGFloat)x{
    CGRect frame=[self frame];
    return frame.origin.x;
}
 
-(void)setX:(CGFloat)x{
    CGRect frame=[self frame];
    frame.origin.x=x;
    [self setFrame:frame];
}
 
-(CGFloat)y{
    CGRect frame=[self frame];
    return frame.origin.y;
}
 
-(void)setY:(CGFloat)y{
    CGRect frame=[self frame];
    frame.origin.y=y;
    return [self setFrame:frame];
}
 
- (CGFloat)left;
{
    return CGRectGetMinX([self frame]);
}
 
- (void)setLeft:(CGFloat)x;
{
    CGRect frame = [self frame];
    frame.origin.x = x;
    [self setFrame:frame];
}
 
- (CGFloat)top;
{
    return CGRectGetMinY([self frame]);
}
 
- (void)setTop:(CGFloat)y;
{
    CGRect frame = [self frame];
    frame.origin.y = y;
    [self setFrame:frame];
}
 
- (CGFloat)right;
{
    return CGRectGetMaxX([self frame]);
}
 
- (void)setRight:(CGFloat)right;
{
    CGRect frame = [self frame];
    frame.origin.x = right - frame.size.width;
 
    [self setFrame:frame];
}
 
- (CGFloat)bottom;
{
    return CGRectGetMaxY([self frame]);
}
 
- (void)setBottom:(CGFloat)bottom;
{
    CGRect frame = [self frame];
    frame.origin.y = bottom - frame.size.height;
    [self setFrame:frame];
}
 
- (CGFloat)centerX;
{
    return [self center].x;
}
 
- (void)setCenterX:(CGFloat)centerX;
{
    [self setCenter:CGPointMake(centerX, self.center.y)];
}
 
- (CGFloat)centerY;
{
    return [self center].y;
}
 
- (void)setCenterY:(CGFloat)centerY;
{
    [self setCenter:CGPointMake(self.center.x, centerY)];
}
 
- (CGFloat)width;
{
    return CGRectGetWidth([self frame]);
}
 
- (void)setWidth:(CGFloat)width;
{
    CGRect frame = [self frame];
    frame.size.width = width;
    [self setFrame:CGRectStandardize(frame)];
}
 
- (CGFloat)height;
{
    return CGRectGetHeight([self frame]);
}
 
- (void)setHeight:(CGFloat)height;
{
    CGRect frame=[self frame];
    frame.size.height = height;
    [self setFrame:CGRectStandardize(frame)];
}

- (void)setCornerRadius:(CGFloat)radius {
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

-(void)makeCornerPos:(UIRectCorner)cPos cornerRadius:(CGFloat)cornerRadius {
    CGRect rect = self.bounds;
    CGSize radio = CGSizeMake(cornerRadius, cornerRadius);//圆角尺寸
    //UIRectCorner corner = UIRectCornerBottomLeft | UIRectCornerBottomRight;//这只圆角位置
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:cPos cornerRadii:radio];
    CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//创建shapelayer
    masklayer.frame = self.bounds;
    masklayer.path = path.CGPath;//设置路径
    self.layer.mask = masklayer;
}

-(void)makeCornerLine:(UIRectCorner)cPos
         cornerRadius:(CGFloat)cornerRadius
            lineWidth:(CGFloat)lineWidth
            lineColor:(CGColorRef)lineColor {
    CGRect rect = self.bounds;
    CGSize radio = CGSizeMake(cornerRadius, cornerRadius);//圆角尺寸
    //UIRectCorner corner = UIRectCornerBottomLeft | UIRectCornerBottomRight;//这只圆角位置
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:cPos cornerRadii:radio];
    path.lineWidth = lineWidth;
    CAShapeLayer *borderlayer = [[CAShapeLayer alloc]init];//创建shapelayer
    borderlayer.frame = self.bounds;
    //borderlayer.borderColor = lineColor;
    borderlayer.strokeColor = lineColor;
    borderlayer.fillColor = UIColor.clearColor.CGColor;
    borderlayer.path = path.CGPath;//设置路径
    [self.layer addSublayer:borderlayer];
}

- (UIViewController *)currentVC  {
    UIResponder *next = self.nextResponder;
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    } while(next != nil);
    
    return nil;
}


- (CAShapeLayer *)drawDashedLineFromPoint:(CGPoint)fromPoint
                                  toPoint:(CGPoint)toPoint
                              lineCgColor:(CGColorRef)lineCgColor
                              linePattern:(NSArray<NSNumber *> *)linePattern
                                lineWidth:(CGFloat)lineWidth; {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    // Creating a path
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:fromPoint];
    [path addLineToPoint:toPoint];
    
    // Assigning the path to the layer
    shapeLayer.path = [path CGPath];
    
    // Setting the line pattern
    shapeLayer.lineDashPattern = linePattern;
    
    // Line color and width
    shapeLayer.strokeColor = lineCgColor;
    shapeLayer.lineWidth = lineWidth;
    
    shapeLayer.name = @"dashedLine";
    
    // Adding the layer to the view
    [self.layer addSublayer:shapeLayer];
    
    return shapeLayer;
}

@end
